#region File Description
//-----------------------------------------------------------------------------
// SpinningTriangleControl.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
#endregion

namespace WinFormsGraphicsDevice
{
    /// <summary>
    /// Example control inherits from GraphicsDeviceControl, which allows it to
    /// render using a GraphicsDevice. This control shows how to draw animating
    /// 3D graphics inside a WinForms application. It hooks the Application.Idle
    /// event, using this to invalidate the control, which will cause the animation
    /// to constantly redraw.
    /// </summary>
    class SpinningTriangleControl : GraphicsDeviceControl
    {
        VertexDeclaration vertexDeclaration;
        BasicEffect effect;
        Stopwatch timer;
        SpriteBatch spriteBatch;
        SpriteFont font;
        ContentManager content;
        int topTri = -1;
        int selectedTri = 0;
        Texture2D sprite;
        Point mPos = new Point(0, 0);
        Point lastmPos = new Point(0, 0);

        public float offset = 0;
        public string testText;

        // Vertex positions and colors used to display a spinning triangle.
        public Triangle[] triangles;

        /// <summary>
        /// Initializes the control.
        /// </summary>
        protected override void Initialize()
        {
            
            // Create our vertex declaration.
            vertexDeclaration = new VertexDeclaration(GraphicsDevice,
                                                VertexPositionColor.VertexElements);

            // Create our effect.
            effect = new BasicEffect(GraphicsDevice, null);

            effect.VertexColorEnabled = true;

            // Start the animation timer.
            timer = Stopwatch.StartNew();

            // Hook the idle event to constantly redraw our animation.
            Application.Idle += delegate { Invalidate(); };

            // Text stuff
            content = new ContentManager(Services, "Content");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = content.Load<SpriteFont>("Arial");

            triangles = new Triangle[10];
            CreateNewTriangle();

            sprite = content.Load<Texture2D>("rect");
        }

        /// <summary>
        /// Draws the control.
        /// </summary>
        protected override void Draw()
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Spin the triangle according to how much time has passed.
            float time = (float)timer.Elapsed.TotalSeconds;

           /* float yaw = time * 0.7f;
            float pitch = time * 0.8f;
            float roll = time * 0.9f;
            */
            // Set transform matrices.
            float aspect = GraphicsDevice.Viewport.AspectRatio;

            effect.World = Matrix.CreateFromYawPitchRoll(0, 0, 0);

            effect.View = Matrix.CreateLookAt(new Vector3(0, 0, -5),
                                              Vector3.Zero, Vector3.Up);

            effect.Projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 10);

            // Set renderstates.
            GraphicsDevice.RenderState.CullMode = CullMode.None;

            GraphicsDevice.VertexDeclaration = vertexDeclaration;

            // Draw the triangle.
            spriteBatch.Begin();

            foreach (Triangle tri in triangles)
            {
                if (tri != null && tri.alive)
                {
                    tri.Draw(spriteBatch, sprite);
                }
            }

            // Text stuff
            string message = "Please type into the box below and I'll try \nmy best to draw it here!\n" + testText;
            spriteBatch.DrawString(font, message, new Vector2(23, 36), Color.White);

            // Draw mouse position
            message = mPos.X.ToString() + ", " + mPos.Y.ToString();
            spriteBatch.DrawString(font, message, new Vector2(23, 106), Color.White);

            spriteBatch.End();

            // Keep old mousePosition
            lastmPos = mPos;
        }

        public void CreateNewTriangle()
        {
            if (topTri < 10)
            {
                topTri++;
                triangles[topTri] = new Triangle();
                triangles[topTri].alive = true;
            }
        }

        public void CheckMouse(Point mousePos, bool tryDrag)
        {
            mPos = mousePos;
            for (int i = 0; i < 10; i++)
            {
                if (triangles[i] == null)
                    break;

                if (triangles[i].CheckSelect(mousePos))
                    if (tryDrag)
                        triangles[i].Drag(lastmPos, mPos);
            }
        }
    }
}
